#include "GAF_FlashAction.h"
#include "GAF_Platform.h"

namespace GAF
{
	FlashAction::FlashAction()
	{
		m_nTimes		= 0;
		m_ttLast		= 0;
		m_isHide		= true;
		m_isHostVisible		= false;
	}

	FlashAction::~FlashAction()
	{
	}

	void FlashAction::SetIntent(ActionIntent* pIntent)
	{
		GAF_CHECK(pIntent != NULL);

		FlashIntent* _pFlashIntent = (FlashIntent*)pIntent;

// 		m_Intent.Reset();
		SYS_MEMCPY(&m_Intent, _pFlashIntent, sizeof(FlashIntent));
		m_Intent.CopyControl(_pFlashIntent);
// 		m_Intent.ttHideTime = _pFlashIntent->ttHideTime;
// 		m_Intent.ttShowTime = _pFlashIntent->ttShowTime;
// 		m_Intent.isFadeIn = _pFlashIntent->isFadeIn;
// 		m_Intent.isFadeOut = _pFlashIntent->isFadeOut;
// 		m_Intent.nTimes = _pFlashIntent->nTimes;
	}

	void FlashAction::FlashFast(UINT nTimes, TimeTicks ttTime, bool isFade)
	{
		Flash(nTimes, ttTime >> 1, ttTime >> 1, isFade);
	}

	void FlashAction::Flash(UINT nTimes, TimeTicks ttHideTime, TimeTicks ttShowTime, bool isFade)
	{
		if(ttHideTime + ttShowTime == 0)
			return;

		FlashIntent _intent;
		_intent.nTimes = nTimes;
		_intent.ttHideTime = ttHideTime;
		_intent.ttShowTime = ttShowTime;
		_intent.isFadeIn = isFade;
		_intent.isFadeOut = isFade;
		SetIntent(&_intent);

		Play();
	}

	bool FlashAction::Playing(Bundle* pbdlParam)
	{
		if(GetState_a() == PLAYER_STOP)
		{
			m_nTimes = 0;
			m_isHostVisible = GetHostGraphics_a()->GetVisible_a();

			if(GetHostGraphics_a()->GetVisible_a())
				ReadyHide();
			else
				ReadyShow();
		}

		m_ttLast = GAF_pTHEPLATFORM->GetTimeTicks();

		return true;
	}

	bool FlashAction::Running(TimeTicks ttUsed, bool& isFinish)
	{
		TimeTicks _ttNow = GAF_pTHEPLATFORM->GetTimeTicks();

		if(m_isHide)
		{ // 隐
			if(_ttNow - m_ttLast >= m_Intent.ttHideTime)
			{
// 				isFinish = true;

				// 次数判断
				if(!DoTimes())
				{
					if(m_Intent.ttHideTime > 0)
						GetHostGraphics_a()->SetVisible_a(false);
					return false;
				}

				// 转为显
// 				if(m_Effect.ttShowTime >= 0)
				{
					// 准备显
					ReadyShow();
				}
				m_ttLast = _ttNow;
			}
			else
			{
				if(m_Intent.isFadeOut && m_Intent.ttHideTime > 0)
				{ // 渐隐
					GetHostGraphics_a()->SetVisible_a(true);
					float fStep = (float)255 / (float)(m_Intent.ttHideTime);
					float fAlpha = 255 - (float)(_ttNow - m_ttLast) * fStep;
					GetHostGraphics_a()->SetAlpha_a((BYTE)fAlpha);
				}
			}
		}
		else
		{ // 显
			if(_ttNow - m_ttLast >= m_Intent.ttShowTime)
			{
// 				isFinish = true;

				// 次数判断
				if(!DoTimes())
				{
					if(m_Intent.ttShowTime > 0)
						GetHostGraphics_a()->SetVisible_a(true);
					return false;
				}

				// 转为隐
// 				if(m_Effect.ttHideTime >= 0)
				{
					// 准备隐
					ReadyHide();
				}
				m_ttLast = _ttNow;
			}
			else
			{
				if(m_Intent.isFadeIn && m_Intent.ttShowTime > 0)
				{ // 渐显
					GetHostGraphics_a()->SetVisible_a(true);
					float fStep = (float)255 / (float)(m_Intent.ttShowTime);
					float fAlpha = (float)(_ttNow - m_ttLast) * fStep;
					GetHostGraphics_a()->SetAlpha_a((BYTE)fAlpha);
				}
			}
					
		}

		return true;
	}

	bool FlashAction::Stopping()
	{
		GetHostGraphics_a()->SetAlpha_a(255);
		return true;
	}

	bool FlashAction::DoTimes()
	{
		m_nTimes++;

		if(m_Intent.nTimes != 0)
		{
			if(m_nTimes % 2 == 0)
			{
				if((m_nTimes >> 1) >= m_Intent.nTimes)
					return false;
			}
		}
		return true;
	}

	void FlashAction::ReadyShow()
	{
		if(m_Intent.isFadeIn)
		{
			GetHostGraphics_a()->SetVisible_a(false);
			GetHostGraphics_a()->SetAlpha_a(0);
		}
		else
			GetHostGraphics_a()->SetVisible_a(true);

		m_isHide = false;
	}

	void FlashAction::ReadyHide()
	{
		if(m_Intent.isFadeOut)
		{
			GetHostGraphics_a()->SetVisible_a(true);
			GetHostGraphics_a()->SetAlpha_a(255);
		}
		else
			GetHostGraphics_a()->SetVisible_a(false);

		m_isHide = true;
	}
}
